//
//  SDPageDataPool.m
//  SDDataTest
//
//  Created by sodao on 12-9-27.
//  Copyright (c) 2012年 sodao. All rights reserved.
//

#import "SDPageDataPool.h"

@implementation SDPageDataPool
@synthesize lastObject;
@synthesize fristObject;
@synthesize pagePoolName;
@synthesize lastUpdateTime;
@synthesize pageValueArray;
@synthesize pageIndex;
@synthesize count;
@synthesize pageSize;
@synthesize hasNextPage;

-(id)lastObject{
    return [self.pageValueArray lastObject];
}
-(id)fristObject{
    return [self.pageValueArray objectAtIndex:0];
}

-(int)count{
    return [self.pageValueArray count];
}

-(id)initWithArray:(NSArray*)array
           pageIndex:(int)index
            lastDate:(NSDate*)date
                name:(NSString*)name{
    self = [super init];
    if (!!self) {
        self.pageValueArray = [[NSMutableArray alloc] initWithArray:array];
        self.lastUpdateTime = date;
        self.pagePoolName = name;
        self.pageIndex = index;
        self.pageSize = 30;
        self.pageIndex = 0;
        hasNextPage = YES;
    }
    return self;
}

-(void)addValues:(NSArray*)array{
    [self.pageValueArray addObjectsFromArray:array];
}

-(NSArray*)getPageData:(int)index size:(int)pagesize{
    int firstIndex = index*pagesize;
    if (firstIndex>self.count) {
        return nil;
    }else{
        NSMutableArray* newArray = [NSMutableArray array];
        int lastIndex;
        if (firstIndex+pagesize>count) {
            lastIndex = count;
        }else{
            lastIndex = firstIndex+pagesize;
        }
        for (int i = firstIndex; i<lastIndex; i++) {
            [newArray addObject:[self.pageValueArray objectAtIndex:i]];
        }
        return newArray;
    }
}

-(void)cleanDataPool{
    [self.pageValueArray removeAllObjects];
}
-(id)objectAtIndex:(int)index{
    return [self.pageValueArray objectAtIndex:index];
}
-(int)indexOfObject:(id)obj{
    return [self.pageValueArray indexOfObject:obj];
}
-(void)insertObject:(id)object atIndex:(NSUInteger)index{
    [self.pageValueArray insertObject:object atIndex:index];
}
-(void)removeObject:(id)obj{
    [self.pageValueArray removeObject:obj];
}
-(void)removeObjectAtIndex:(int)index{
    [self.pageValueArray removeObjectAtIndex:index];
}
@end
